//导入axios
import axios from "axios";
import qs from "qs";
import { showToast } from "vant";

const request = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_BASEPATH,
    timeout: 10000,
    withCredentials: false,
    paramsSerializer: function (params) {
        return qs.stringify(params, { indices: false });
    },
});

//请求前拦截器
request.interceptors.request.use(function (config) {
    //请求头 添加token
    config.headers.authorization = `Bearer ${sessionStorage.getItem("amp_token")}`;

    return config;
});

request.interceptors.response.use(function (response) {
    //接口错误
    if (response.data.code !== 1) {
        showToast({
            message: response.data.msg,
            duration: 3000,
        });
        //弹出错误信息
        throw new Error(response.data.msg);
    }

    return response.data;
});

export default request;
